<?php
/**
 * Session data transfer
 * Useful if you don't want to remember a long unique session name
 * or you want to do something within the functions
 *
 * @author sm-Dezs
 */
namespace Session;
class SDT {

	/**
	 * Session name
	 * @var string
	 */
	protected $_name;

	/**
	 * Sets session name for the class
	 * @param string $name
	 */
	private function __construct($name) {
		$this->_name = $name;
	}

	/**
	 * Factory method for SDT class
	 * @param string $name
	 * @return \Session\SDT
	 */
	public static function init($name = null) {
		if (is_null($name)) {
			$name = get_called_class() . "_SESSION";
		}
		return new static($name);
	}

	/**
	 * Is session set
	 * @return bool
	 */
	public function is() {
		return isset($_SESSION[$this->_name]);
	}

	/**
	 * Return stored session data
	 * @return mixed
	 */
	public function get() {
		return $this->is() ? $_SESSION[$this->_name] : null;
	}

	/**
	 * Set session data
	 * @param string $data
	 * @return void
	 */
	public function set($data) {
		$_SESSION[$this->_name] = $data;
	}

	/**
	 * Delete session
	 * @return void
	 */
	public function del() {
		if ($this->is()) {
			unset($_SESSION[$this->_name]);
		}
	}

}
?>
